package com.tevintao.marketingsystem.xposted;

import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.tevintao.marketingsystem.log.LogManager;

import java.lang.reflect.Method;

import de.robv.android.xposed.XC_MethodHook;


public class HookWebView {

    public static final String ACTION_GET_LAST_HTML = "ACTION_GET_LAST_HTML";
    public static final String KEY_LAST_HTML = "KEY_LAST_HTML";

    public static final String ACTION_GET_ELEMENT_RECT = "ACTION_GET_ELEMENT_RECT";
    public static final String KEY_GET_ELEMENT_RECT = "KEY_GET_ELEMENT_RECT";


    public static final String ACTION_DO_JAVA_SCRIPT = "ACTION_DO_JAVA_SCRIPT";
    public static final String KEY_DO_JAVA_SCRIPT = "KEY_DO_JAVA_SCRIPT";
    private static HookWebView ourInstance = new HookWebView();

    public static HookWebView getInstance() {
        return ourInstance;
    }

    private HookWebView() {
    }

    public static String mLastHtml = "";
    public static String mLastRect = "";

    final class InJavaScriptLocalObj {
        @JavascriptInterface
        public void showSource(String html) {
            mLastHtml = html;
        }

        @JavascriptInterface
        public void showLink(String html) {
            LogManager.getInstance().logInfo("InJavaScriptLocalObj showLink " + html);
        }

        @JavascriptInterface
        public void setRect(String rect) {
            LogManager.getInstance().logInfo("HookWebView setRect " + rect);
            mLastRect = rect;
        }
    }

    private static WebView lastWebView = null;

    public static final String TAG_GET_LAST_WEB_VIEW_HTML = "tag_get_last_web_view_html";
    public static final String TAG_DO_SCRIPT = "tag_do_script";

    public boolean loadHtml() {
        if (null != lastWebView) {
            lastWebView.loadUrl("javascript:window.local_obj.showSource('<head>'+" +
                    "document.getElementsByTagName('html')[0].innerHTML+'</head>');");
        } else {
            return false;
        }
        return true;
    }

    public boolean doScript(String script) {
        if (null != lastWebView) {
            lastWebView.loadUrl(script);
        } else {
            return false;
        }
        return true;
    }

    public void hookWebView() {
        HookDeviceModule.hookAllMethodByName(WebView.class, "setWebViewClient", new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                LogManager.getInstance().logInfo("hookWebView setWebViewClient setWebViewClient webview class = " + param.thisObject.getClass().getName());
                if (param.thisObject instanceof WebView) {
                    LogManager.getInstance().logInfo("param.thisObject instanceof WebView");
                    lastWebView = (WebView) param.thisObject;
                    lastWebView.addJavascriptInterface(new InJavaScriptLocalObj(), "local_obj");
                }
            }
        });

//        HookDeviceModule.hookAllMethodByName(WebView.class, "addJavascriptInterface", new XC_MethodHook() {
//            @Override
//            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                super.beforeHookedMethod(param);
//                LogManager.getInstance().logInfo("addJavascriptInterface ---------- " + param.thisObject);
//                for (Object po : param.args) {
//                    LogManager.getInstance().logInfo("addJavascriptInterface params = " + po);
//                }

//                if (param.thisObject instanceof WebView) {
//                    HookDeviceModule.hookAllMethodByName(param.thisObject.getClass(), "addJavascriptInterface", new XC_MethodHook() {
//                        @Override
//                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                            super.beforeHookedMethod(param);
//                            LogManager.getInstance().logInfo("addJavascriptInterface param.thisObject " + param.thisObject);
//                            for (Object po : param.args) {
//                                LogManager.getInstance().logInfo("addJavascriptInterface params = " + po);
//                            }
//                        }
//                    });
//                }


//            }
//        });

    }
}